package cn.bdqn.action;

import java.util.Map;

import cn.bdqn.pojo.Users;
import cn.bdqn.service.UsersService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private Users user;

	//注入userservice
	private UsersService  usersService;
	
	//解耦获取session，request
	ActionContext  context = ActionContext.getContext();
	Map  session = context.getSession();
	Map request = (Map) context.get("request");
	
	//验证
	@Override
	public void validate() {
		if(user.getUserCode()==null || user.getUserCode().length()==0){
			addFieldError("user.userCode", "用户编码不能为空！");
		}
		if(user.getPwd()==null || user.getPwd().length()==0){
		addFieldError("user.pwd", "用户密码不能为空！");	
		}
	}
	
	
	//登录
	public String login(){
		Users u = usersService.doLogin(user.getUserCode(), user.getPwd());
		if(u!=null){
			session.put("user", user);
			return  SUCCESS;
		}else {
		    request.put("error","用户编号和密码不匹配！！");
		    return  ERROR;
		}
		
	}
	
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}


	public UsersService getUsersService() {
		return usersService;
	}


	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}
	

}
